<?php 
header('Content-Type: text/plain');
error_reporting(E_ALL);

$param = include('../../../../LocalConfiguration.php');
mysql_connect($param['DB']['host'], $param['DB']['username'], $param['DB']['password']) or die('Not connected : ' . mysql_error());
mysql_select_db($param['DB']['database']) or die ("Can't use 'mkg_hospitality' : " . mysql_error());
// mysql_query("SET NAMES 'utf8'") or die(mysql_error() . "\nNAMES" );;
mysql_set_charset('utf8');
// mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'") or die(mysql_error() . "\ncharset" );;;



$file = 'update_static.csv';
$h = fopen($file, 'r');

$store = 6;
$table_pos = 'tx_mountains_domain_model_position';
$table_som = 'tx_mountains_domain_model_mountain';
$table_mm_pays = 'tx_mountains_mountain_country_mm';

while($data = fgetcsv($h, 0, ',', '"')) {
	unset($nom, $altitude, $pays_iso, $pays, $ile, $top, $fait, $lat, $lng, $wiki, $chaine, $chaine_wiki);
	list($nom, $altitude, $pays_iso, $pays,	$ile, $top, $fait, $lat, $lng, $wiki, $chaine, $chaine_wiki) = $data;
	
	echo $nom . "\n";
	$nom = addslashes($nom);
	$pays_iso = explode(',', $pays_iso);
	$nombre_pays = count($pays_iso);
	$i = 0;
	
	// $fait = strptime($fait, '%D');
	// $fait = mktime($fait['tm_hour'], $fait['tm_min'], $fait['tm_sec'], $fait['tm_mon'] + 1, $fait['tm_mday'], $fait['tm_year'] + 1900);
	if(strlen($fait) > 0) {
		$fait = explode('/', $fait);
		$fait = mktime(0, 0, 0, $fait[1], $fait[0], $fait[2] + 2000);
	} else {
		// break; 
	}

	//position
	$req_pos = "INSERT into $table_pos SET pid='$store', lat='$lat', lng='$lng', altitude='$altitude'";
	$res_pos = mysql_query($req_pos) or die(mysql_error() . "\n" . $req_pos);
	$id_pos = mysql_insert_id();
	
	//sommet
	$req_som = "INSERT into $table_som SET pid=$store, name='$nom', altitude='$altitude', done='$fait', position=$id_pos, country=$nombre_pays, crdate='". time() . "'";
	$res_som = mysql_query(utf8_encode($req_som)) or die(mysql_error() . "\n" . $req_som);
	$id_som = mysql_insert_id();
	echo $req_som . "\n";
	
	//pays
	foreach($pays_iso as $code) {
		// if($code == '') { exit; }
		$req_pays = "SELECT uid FROM static_countries WHERE cn_iso_2='$code'";
		$res_pays = mysql_query($req_pays) or die(mysql_error() . "\n" . $req_pays);
		$uid_pays = mysql_fetch_array($res_pays);
		$uid_pays = $uid_pays[0];
		
		$req_i_pays = "INSERT INTO $table_mm_pays SET uid_local=$id_som, uid_foreign=$uid_pays, sorting=". $i++ ."";
		$res_i_pays = mysql_query($req_i_pays) or die(mysql_error() . "\n" . $req_i_pays);
	}
	
	// var_dump($pays_iso);
	// var_dump($list_pays);
	// unset($list_pays);
	


}


















